package com.shop.user.atom.atomsvc.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.common.entity.user.UserPay;
import com.shop.common.model.InputBean;
import com.shop.common.model.ServiceBean;
import com.shop.common.model.param.user.UserPayLogInput;
import com.shop.common.model.result.user.UserPayLogOutput;
import com.shop.common.model.result.user.UserPayOutput;
import com.shop.common.tmpl.BaseService;
import com.shop.user.atom.atomsvc.IUserPayAtomSvc;
import com.shop.user.atom.dao.UserPayLogMapper;
import com.shop.user.atom.dao.UserPayMapper;
import com.shop.user.conf.util.DataState;
import com.shop.user.conf.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class UserPayAtomSvc extends BaseService implements IUserPayAtomSvc {
    @Autowired
    UserPayMapper userPayMapper;
    @Autowired
    UserPayLogMapper userPayLogMapper;

    @Override
    public ServiceBean<Integer> insertUserPay(InputBean<UserPay> payInputBean) {
        UserPay userPay = payInputBean.getParam();
        userPay.setCreateTime(DateUtil.newNowDate());
        userPay.setStatus(DataState.Normal.getValue());
        userPay.setBalance(new BigDecimal(0));
        userPay.setAliPay("pulnju4691@sandbox.com");
        userPay.setIntegral(200);
        int num = userPayMapper.insertSelective(userPay);
        if (opSuccess(num)) {
            UserPayLogInput userPayLog = UserPayLogInput.newUserPayLog(userPay.getUserId(), 200, 0, 1, "用户注册赠送积分");
            userPayLogMapper.insertSelective(userPayLog);
            return resultSuccess("注册成功");
        }
        return resultFailed("注册失败");
    }

    @Override
    public ServiceBean<UserPayOutput> selectUserPay(Integer userId) {
        UserPayOutput userPay = userPayMapper.selectByUserId(userId);
        if (userPay == null) {
            return resultFailed("查询失败");
        }
        return resultModel(true, "", userPay);
    }

    @Override
    public ServiceBean<String> updateUserPay(UserPay payParamModel) {
        int num = userPayMapper.updateByPrimaryKeySelective(payParamModel);
        if (opSuccess(num)) {
            return resultSuccess("更新成功");
        }
        return resultFailed("更新失败");
    }

    @Override
    public ServiceBean<String> deleteUserPay(InputBean<UserPay> inputBean) {
        UserPay userPay = inputBean.getParam();
        int num = userPayMapper.deleteByUserId(userPay.getUserId());
        if (opSuccess(num)) {
            return resultSuccess("成功");
        }
        return resultFailed("失败");
    }

    @Override
    public ServiceBean<Integer> addUserPayLog(UserPayLogInput input) {
        int i = userPayLogMapper.insertSelective(input);
        if (opSuccess(i)) {
            return resultData(input.getId());
        }
        return resultFailed();
    }

    @Override
    public ServiceBean<List<UserPayLogOutput>> qryUserPayLogs(UserPayLogInput input) {
        List<UserPayLogOutput> list = userPayLogMapper.qryUserPayLogs(input);
        if (!list.isEmpty()) {
            resultData(list);
        }
        return resultFailed();
    }

    @Override
    public ServiceBean<List<UserPayLogOutput>> qryUserPayLog4P(UserPayLogInput input) {
        PageHelper.startPage(input.getPage(), input.getPageSize());
        List<UserPayLogOutput> list = userPayLogMapper.qryUserPayLogs(input);
        if (list.isEmpty()) {
            return resultFailed();
        }
        PageInfo<UserPayLogOutput> info = new PageInfo<>(list);
        ServiceBean<List<UserPayLogOutput>> serviceBean = new ServiceBean<>();
        serviceBean.setData(info.getList());
        serviceBean.setMaxSize(info.getTotal());
        return serviceBean;
    }

    @Override
    public ServiceBean<Integer> delUserPayLogById(Integer id) {
        int i = userPayLogMapper.deleteByPrimaryKey(id);
        if (opSuccess(i)) {
            return resultData(i);
        }
        return resultFailed();
    }
}
